package com.trkj.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.dao.jpa.IBedTableDao;
import com.trkj.dao.jpa.IExchangebedTableDao;
import com.trkj.dao.jpa.ILiveinrecordTableDao;
import com.trkj.dao.jpa.ISickroomtypeTableDao;
import com.trkj.dao.mybatis.SickroomTableMapper;
import com.trkj.pojos.BedTable;
import com.trkj.pojos.ExchangebedTable;
import com.trkj.pojos.LiveinrecordTable;
import com.trkj.vo.SickroomTypeVo;
@Service
@Transactional
public class SickroomService {
	@Autowired
	private ISickroomtypeTableDao iSickroomtypeDao;
	@Autowired
	private SickroomTableMapper sickroomTableMapper;
	@Autowired
	private IExchangebedTableDao iExchangebedTableDao;
	@Autowired
	private ILiveinrecordTableDao iLiveinrecordTableDao;
	@Autowired
	private IBedTableDao iBedTableDao;
	
	/**
	 * 多条件查询病床
	 * @param map
	 * @return
	 */
	public Map<String, Object> findSickBedByManyIf(Map<String,Object> map,int page,int rows){
		
		Page<Map<String,Object>> p = PageHelper.startPage(page, rows, true);
		List<Map<String,Object>> list = sickroomTableMapper.manyIfQuerySickroom(map);
		Map<String, Object> resultMap =new HashMap<>();
		resultMap.put("rows", list);
		resultMap.put("total", p.getTotal());
		return resultMap;
	}
	
	/**
	 * 查询所有病房类型
	 * @return
	 */
	public List<SickroomTypeVo> queryAllSickroomType(){
		List<SickroomTypeVo> list 
		= (List<SickroomTypeVo>) iSickroomtypeDao.findallSickroomType();
		return list;
	}
	/**
	 * 创建新的病房更换ID
	 * @return
	 */
	public String createExchangeBedID(){
		
		String newExchangeBedID = "";
		//将当前日期格式化为纯数字
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String str = sdf.format(new Date()).substring(2, 8);
		String LikeID = "%" + str + "%";
		List<String> list = iExchangebedTableDao.queryExchangeBedID(LikeID);
		
		if(list.isEmpty()){
			newExchangeBedID = "RB" + str +"0001";
		}else{
			List<Integer> lists = new ArrayList<>();
			for(int i=0;i<list.size();i++){
				lists.add(Integer.valueOf(list.get(i).toString().substring(8, 12)));
			}
			//Collections.max取最大值再加1，拼接住院号
			 int next = Collections.max(lists).intValue()+1;
			 String nextID = String.format("%04d", next);
			 newExchangeBedID = "RB" + str + nextID;
		}
		return newExchangeBedID;
	}
	/**
	 * 更换床位
	 * @param exchangebedTable
	 * @return
	 */
	public boolean newExchangeBed(ExchangebedTable exchangebedTable){
		try {
			LiveinrecordTable liveT = iLiveinrecordTableDao.findOne(exchangebedTable.getLiveid5());
			//与新床位建立关系并修改状态
			Long newbedid = Long.parseLong(exchangebedTable.getNewbedid());
			BedTable newBed = iBedTableDao.findOne(newbedid);
			liveT.setBedTable(newBed);
			newBed.setBedstatus("已住");
			iBedTableDao.save(newBed);
			iLiveinrecordTableDao.save(liveT);
			//修改旧床位的状态
			Long oldbedid = Long.parseLong(exchangebedTable.getOldbedid());
			BedTable oldBed = iBedTableDao.findOne(oldbedid);
			oldBed.setBedstatus("待打扫");
			iBedTableDao.save(oldBed);
			//建立记录表
			iExchangebedTableDao.save(exchangebedTable);
			return true;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 打扫病房
	 * @param bid
	 * @return
	 */
	public boolean sweepBed(Long  bid){
		try {
			BedTable bedTable = iBedTableDao.findOne(bid);
			bedTable.setBedstatus("空闲");
			iBedTableDao.save(bedTable);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 锁定病房
	 * @param bid
	 * @return
	 */
	public boolean lockBed(Long bid,String status){
		try {
			BedTable bedTable = iBedTableDao.findOne(bid);
			bedTable.setBedstatus(status);
			iBedTableDao.save(bedTable);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 根据病房号查询病人
	 * @param bedID
	 * @return
	 */
	public Map<String,Object> queryPatienInfoByBedID(Integer bedID){
		return sickroomTableMapper.queryPatientByBid(bedID);
	}

}
